//
//  TravelViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/17.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "TravelViewController.h"
#import "TravelCell.h"
#import "TravelLeftCell.h"
#import "TravelViewModel.h"
#import "RoutePlanningViewController.h"
#import "OrderDetailViewController.h"

@interface TravelViewController () 

@property (strong, nonatomic) TravelViewModel *travelViewModel;

@property (weak, nonatomic) IBOutlet UITableView *mainTable;
@property (weak, nonatomic) IBOutlet UITableView *leftTable;

@property (strong, nonatomic) IBOutlet UIView *leftBgView;

@property (strong, nonatomic) IBOutlet UIButton *itemBtn;


- (IBAction)itemBtnAction:(UIButton *)sender;
@property (assign, nonatomic) BOOL isOpen;

@property (weak, nonatomic) IBOutlet UIView *itemBgView;

@property (weak, nonatomic) IBOutlet UIView *leftTableHeader;



@end

@implementation TravelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的行程";
    [self setLeftBtnItem];
    
    
    self.travelViewModel = [[TravelViewModel alloc ] init];
    [self.travelViewModel loadData:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [self.mainTable reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg duration:1];
        }
    }];
    
    [self setExtraCellLineHidden:self.mainTable];
    [self setExtraCellLineHidden:self.leftTable];
    
    UIView *line = [[UIView alloc ] initWithFrame:CGRectMake(0, 44, 130, 0.45)];
    line.backgroundColor = UI_RGBA(220, 220, 220, 1);
    [self.leftTableHeader addSubview:line];
    self.leftTable.tableHeaderView = self.leftTableHeader;
    
    self.leftBgView.frame = CGRectMake(-130, 0, 130, [UIScreen height]);
    self.leftBgView.layer.shadowColor   = [UIColor lightGrayColor].CGColor;
    self.leftBgView.layer.shadowOffset  = CGSizeMake(0, 3);
    self.leftBgView.layer.shadowOpacity = 0.8;
    self.leftTable.frame  = CGRectMake(0, 0, 130, [UIScreen height]);
    
}




#pragma mark ---- 列表代理 ----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.mainTable) {
        
        return [self.travelViewModel.mainDataSource count];
    }else{
        return self.travelViewModel.resultList.count;
    }
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    if (tableView == self.mainTable) {
//        return 35;
//    }else{
//        return 0.01;
//    }
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.mainTable) {
        return 80;
    }else{
        return 44;
    }
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    if (tableView == self.mainTable) {
//
//        MyTripEntity *entity = [self.travelViewModel.resultList objectAtIndex:section];
//        
//        UIView *sectionView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, [UIScreen width], 35)];
//        sectionView.backgroundColor = GLOBLE_BACKGROUND_COLOR;
//        UILabel *sectionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(10, 0, [UIScreen width] - 20, 35)];
//        sectionLabel.textAlignment = NSTextAlignmentCenter;
//        sectionLabel.textColor = GLOBLE_TEXT_COLOR;
//        sectionLabel.text = entity.TripDate;
//        sectionLabel.font = [UIFont systemFontOfSize:14];
//        [sectionView addSubview:sectionLabel];
//        return sectionView;
//    }else{
//        UIView *sectionView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, [UIScreen width], 0.01)];
//        return sectionView;
//    }
//}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.mainTable) {
        TravelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelCell"];
   
        
        [cell showModel:[self.travelViewModel.mainDataSource objectAtIndex:indexPath.row]];
        return cell;
    }else{
        TravelLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelLeftCell"];
        MyTripEntity *entity = [self.travelViewModel.resultList objectAtIndex:indexPath.row];
        cell.text.text = entity.TripDate;
        
        cell.countLabel.text = @(entity.TicketCount).stringValue;
        
        if ([self.travelViewModel.selectedTime isEqualToString:cell.text.text]) {
            [cell setBackgroundColor:[UIColor whiteColor]];
        }else{
            [cell setBackgroundColor:[UIColor clearColor]];
        }
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.mainTable) {
        
        
        MyTripDetailEntity *entity = [self.travelViewModel.mainDataSource objectAtIndex:indexPath.row];
        
        
       OrderDetailViewController *orderDetailViewController = [[UIStoryboard storyboardWithName:@"Order" bundle:nil ]  instantiateViewControllerWithIdentifier:@"OrderDetailViewController"];
    
        orderDetailViewController.orderDetailViewModel.TicketOrderId = entity.TicketOrderId;
        
        if (entity.TicketType == 1) {
            orderDetailViewController.isRegularBus = YES;
        }
        
        [self.navigationController pushViewController:orderDetailViewController animated:YES];
        
//        RoutePlanningViewController *routePlanningViewController = (RoutePlanningViewController *)[RoutePlanningViewController getViewControllerFromSbName:@"RoutePlanning" viewControllerName:@"RoutePlanningViewController"];
//        routePlanningViewController.routePlanningStatus = RoutePlanningTravel;
//        routePlanningViewController.OrderDetailId = entity.TicketOrderDetailId;
//        [self.navigationController pushViewController:routePlanningViewController animated:YES];
    }else{
        
        MyTripEntity *entity = [self.travelViewModel.resultList objectAtIndex:indexPath.row];
        self.travelViewModel.selectedTime = entity.TripDate;
        self.travelViewModel.mainDataSource = entity.myTripDetailList;
        [self.leftTable reloadData];
        [self.mainTable reloadData];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if ([scrollView isKindOfClass:[self.mainTable class]] && self.isOpen && scrollView.tag != 10) {
        [self itemBtnAction:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)itemBtnAction:(UIButton *)sender {
    if (self.isOpen) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            WEAKSELF
            [UIView animateWithDuration:0.15 animations:^{
                weakSelf.leftBgView.frame = CGRectMake(-130, 0, 130, [UIScreen height]);
                weakSelf.itemBgView.transform=CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
                [weakSelf.leftBgView removeFromSuperview];
            }];
        });
        
    }else{
        WEAKSELF
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.view addSubview:self.leftBgView];
            [UIView animateWithDuration:0.15 animations:^{
                weakSelf.leftBgView.frame = CGRectMake(0, 0, 130, [UIScreen height]);
                weakSelf.itemBgView.transform=CGAffineTransformMakeRotation(M_PI/2);
            }];
        });
    }
    self.isOpen = !self.isOpen;
}
@end
